﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Big2.CardsLib
{
    public class Deck
    {
        private Card[] cardDeck;                        //deck of cards
        private int currentCard;
        private int lastCard;

        //empty parameter constructor
        //will only contain 52 cards unshuffled
        public Deck()
        {
            cardDeck = new Card[52];                    //array of 52 new cards
            currentCard = 0;                           //init currentCard to 0
            lastCard = 51;                              //init lastCard to 51

            int next = 0;
            for (CardSuit suit = CardSuit.Diamonds; suit <= CardSuit.Spades; suit++)
            {
                for (CardRank rank = CardRank.Ace; rank <= CardRank.King; rank++, next++)
                {
                    cardDeck[next] = new Card(rank, suit);
                }//inner for
            }//outer for
        }//empty parameter constructor

        //shuffle the cards
        public void shuffleDeck()
        {
            Card[] tempDeck = new Card[52];             //create tempdeck to hold the cards
            bool[] picked = new bool[52];               //create array of bools for picking cards
                                                        //default boolean values are set to false

            Random rand = new Random();
            int randNum = 0;
            int currentNum = 0;

            while (currentNum < 52)
            {
                randNum = rand.Next(0, 52);     //generate number 
                if (picked[randNum] == false)
                {
                    tempDeck[currentNum] = cardDeck[randNum];
                    picked[randNum] = true;
                    currentNum++;
                }//if
            }//while

            cardDeck = tempDeck;                        //set cardDeck as newly shuffled deck
        }//shuffleDeck()

        //get specific card
        public Card getCard(int index)
        {
            if(index >= 0 && index < 52)
                return cardDeck[index];
            else
                throw (new System.ArgumentOutOfRangeException("index" + index +
                                                                "Value must be between 0 and 51."));
        }//getCard(int index)

        //deal next card
        public Card getNextCard()
        {
            if (currentCard < lastCard)
                return cardDeck[currentCard++];
            else
                throw (new System.ArgumentOutOfRangeException("currentCard is out of bounds"));
        }//getNextCard()

        //peek next card
        public Card peekNext()
        {
            if (currentCard < lastCard)
            {
                Card temp = cardDeck[currentCard];
                return temp;
            }//if
            else
                throw (new System.ArgumentOutOfRangeException("currentCard is out of bounds"));
        }//Card peekNext()

        //deal last card
        public Card getLastCard()
        {
            if (lastCard > currentCard)
                return cardDeck[lastCard--];
            else
                throw (new System.ArgumentOutOfRangeException("lastCard is out of bounds"));
        }//getLastCard()

        //peek last card
        public Card peekLast()
        {
            if (lastCard > currentCard)
            {
                Card temp = cardDeck[lastCard];
                return temp;
            }//if
            else
                throw (new System.ArgumentOutOfRangeException("lastCard is out of bounds"));
        }//peekLast()

        public bool HasCard()
        {
            return currentCard < lastCard;
        }

    }
}
